package com.zikao.article.controller;

import java.util.*;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zikao.article.entity.ArticleEntity;
import com.zikao.article.service.ArticleService;
import com.zikao.common.vo.TableListVo;
import com.zikao.user.entity.UserEntity;
import com.zikao.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import com.zikao.article.entity.CollectEntity;
import com.zikao.article.service.CollectService;
import com.zikao.common.utils.PageUtils;
import com.zikao.common.utils.R;

/**
 * @author likaisheng
 * @email 1805318928@qq.com
 * @date 2021-02-21 10:55:38
 */
@Controller
@Slf4j
@RequestMapping("article/collect")
public class CollectController {
    @Autowired
    private CollectService collectService;
    @Autowired
    private ArticleService articleService;
    @Autowired
    private UserService userService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @ResponseBody
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = collectService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 按参数获取点赞信息
     */
    @RequestMapping("/selectByMap")
    @ResponseBody
    public R selectByMap(@RequestBody Map<String, Object> params){
        List<CollectEntity> collectList = collectService.selectByMap(params);
        return R.ok().put("collectList", collectList);
    }
    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @ResponseBody
    public R info(@PathVariable("id") Long id){
		CollectEntity collect = collectService.getById(id);

        return R.ok().put("collect", collect);
    }

    @RequestMapping("/listForLayUI")
    @ResponseBody
    public TableListVo listForLayUI(@RequestParam Map<String, Object> params){
        QueryWrapper<ArticleEntity> queryWrapper = new QueryWrapper<>();
        // 是否传入了用户id
        String userIdStr = (String) params.get("userId");
        if (StringUtils.isNotBlank(userIdStr)){
            Long userId = Long.parseLong(userIdStr);
            queryWrapper.eq("user_id",userId);
        }

        PageUtils page = collectService.queryPage(params,queryWrapper);
        TableListVo<CollectEntity> tableListVo = new TableListVo();

        tableListVo.setCode(0);
        tableListVo.setMsg("");
        tableListVo.setCount(page.getTotalCount());

        List<CollectEntity> collectList = (List<CollectEntity>) page.getList();
        List<Long> articleIds = collectList.stream().map((r) -> {
            return r.getArticleId();
        }).collect(Collectors.toList());
        if (collectList.size()>0){
            List<ArticleEntity> articleList = articleService.selectBatchIds(articleIds);
            List<Long> userIds = articleList.stream().map(r -> {
                return r.getUserId();
            }).collect(Collectors.toList());
            List<UserEntity> userList = userService.selectBatchIds(userIds);
            Map<Long, ArticleEntity> articleMap = articleList.stream().collect(Collectors.toMap(v -> v.getId(), v -> v));
            Map<Long, UserEntity> userMap = userList.stream().collect(Collectors.toMap(v -> v.getId(), v -> v));

            for (CollectEntity collect : collectList){
                ArticleEntity article = articleMap.get(collect.getArticleId());
                UserEntity user = userMap.get(article.getUserId());
                collect.setArticleTitle(article.getTitle());
                collect.setArticleUserName(user.getName());
            }
        }

        tableListVo.setData(collectList);
        return tableListVo;
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @ResponseBody
    public R save(@RequestBody CollectEntity collect){
		collectService.save(collect);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/removeObj")
    @ResponseBody
    public R removeObj(@RequestBody CollectEntity collect){
        Map<String,Object> collectMap = new HashMap<>();
        try{
            collectMap.put("article_id",collect.getArticleId());
            collectMap.put("user_id",collect.getUserId());
            collectService.removeByMap(collectMap);
        }catch (Exception e){
            log.error("程序出现错误-removeObj",e);
            return R.error();
        }
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @ResponseBody
    public R update(@RequestBody CollectEntity collect){
		collectService.updateById(collect);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @ResponseBody
    public R delete(@RequestBody Long[] ids){
		collectService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
